//var url = 'http://www.earthtools.org/sun/38.78/-77.39/27/1/99/1';

function sunFeed() {
	this.urlBase = 'http://www.earthtools.org/sun';
	this.lat = '38.78';
	this.lon = '-77.39';
	this.date = '27/1';  // day/month
	this.timezone = '99';
	this.dst = 1;
	this.sunrise = "";
	this.sunset = "";
}

sunFeed.prototype.loadSunrise = function(toObject, rowNum, colNum){
	this.obj = toObject;
	this.rowNum = rowNum;
	this.colNum = colNum;
};

//sunFeed.prototype.watch("sunrise",
//   function (id, oldval, newval) {
//      this.obj.putItem(this.rowNum, this.colNum, newval);
//      debug( "sunrise: " + newval );
//      return newval;
//   });
	
sunFeed.prototype.getFeed = function() {
	if( !this.lat || !this.lon || !this.date ) return;
	var url = [this.urlBase,this.lat,this.lon,this.date,this.timezone,this.dst].join('/');
	debug( 'url: ' +  url );
	_IG_FetchXmlContent(url, this.callback, { refreshInterval: 0 } );
};

sunFeed.prototype.callback = function(response) {
	debug( 'starting callback' );
	if (	response == null ||
			typeof(response) != "object" ||
			response.firstChild == null
		){
		_gel("content_div").innerHTML
			= "<i>Sorry sunrise / sunset data feed broken, try back later.</i>";
		return;
	}
	this.sunrise = response.getElementsByTagName("sunrise")[0].firstChild.nodeValue;
	var x = this.obj;
	x.putItem(this.rowNum, this.colNum, this.sunrise);
	debug( 'this.sunrise: ' + this.sunrise );
	this.sunset = response.getElementsByTagName("sunset")[0].firstChild.nodeValue;
	debug( 'this.sunset: ' + this.sunset );
	debug( 'completing callback' );
};